//
//  StoreManageVC.m
//  dyseller
//
//  Created by 红喇叭 on 2017/8/14.
//  Copyright © 2017年 红喇叭. All rights reserved.
//

#import "StoreManageVC.h"
#import "BaiduMapVC.h"
#import "ShopBaseInfoVC.h"
#import "ForgotPasswordVC.h"
#import "PayOrderViewController.h"
#import <AudioToolbox/AudioToolbox.h>
#import "NocticeSetViewController.h"
#import "myWkWebViewController.h"
#import "PrinterManageVC.h"
#import "FoodManagementVC.h"
#import "MerchantHomeVC.h"
#import "HLBSalesStatisticsVC.h"
#import "HLBReimburseVC.h"

@interface StoreManageVC ()<UITableViewDataSource,UITableViewDelegate>{
    UILabel *_storeName;
    UIImageView *_storeImg;
}

@property(nonatomic,strong)UITableView *tableView;

@end

@implementation StoreManageVC

- (void)viewDidLoad {
    [super viewDidLoad];
    
    [self initView];
    
    UIView *footerView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, SCREEN_W, GETHEIGHT(260))];
    footerView.backgroundColor = UIColorFromRGB(0xEBEBEB);
    
    UIButton *outLogin = [[UIButton alloc] initWithFrame:CGRectMake(GETHEIGHT(50), GETHEIGHT(36), GETWITH(670) , GETHEIGHT(90))];
    outLogin.centerX = footerView.centerX;
    outLogin.titleLabel.font = font14;
    outLogin.titleLabel.textColor = [UIColor whiteColor];
    [outLogin setTitle:@"退出登录" forState:UIControlStateNormal];
    [outLogin setBackgroundImage:[UIImage imageNamed:@"user_btn_default_nor.png"] forState:UIControlStateNormal];
    [outLogin addTarget:self action:@selector(loginOut) forControlEvents:UIControlEventTouchUpInside];
    
    [footerView addSubview:outLogin];
    
    
    
    self.tableView.tableFooterView = footerView;
}

- (void)loginOut{
    [[PPHttpRequsetData shareInstance] requestStyle:Request_POST URL:@"/ShopApi/Login/loginOut" parameter:@{} successBlock:^(PPHttpRequest *request, id responseObj) {
        [userDefaults removeObjectForKey:@"agentInfo"];
        [userDefaults removeObjectForKey:@"admin_id"];
        [userDefaults removeObjectForKey:@"guid"];
       // [userDefaults removeObjectForKey:@"HLBdeviceToken"];
        [[AppDelegate delegate] LoginVC];
    } failBlock:^(PPHttpRequest *request, NSString *errMsg) {
        [AlertUtils showToastWithMessage:errMsg inView:self.view hideAfterDelay:2.0 beIndeter:NO];
    }];

}

- (UITableView *)tableView{
    if(_tableView == nil){
        _tableView = [[UITableView alloc] initWithFrame:CGRectMake(0,0, SCREEN_W, SCREEN_H-TabBarItemHeight) style:UITableViewStyleGrouped];
        _tableView.delegate = self;
        _tableView.dataSource = self;
        //_tableView.scrollEnabled = NO;
        _tableView.backgroundColor = UIColorFromRGB(0xEBEBEB);
        _tableView.showsVerticalScrollIndicator = NO;
        if(SCREEN_H == 812){
             _tableView.contentInset = UIEdgeInsetsMake(-46, 0, 0, 0);
        }
        //else{
//            // _tableView.contentInset = UIEdgeInsetsMake(-22, 0, 0, 0);
//        }
        [_tableView registerClass:[UITableViewCell class] forCellReuseIdentifier:@"Cell"];
        [self.view addSubview:_tableView];
    }
    //店铺管理，基本信息，店铺地图
    //系统 通知列表 关于我们
    return _tableView;
}

- (void)viewWillAppear:(BOOL)animated{
      NSDictionary *useInfo = [userDefaults objectForKey:@"agentInfo"];
     [_storeImg sd_setImageWithURL:[NSURL URLWithString:useInfo[@"headimgurl"]] placeholderImage:[UIImage imageNamed:@"home_head_icon_default.png"]];
     _storeName.text = useInfo[@"agent_name"];
    [super viewWillAppear:animated];
      [self.navigationController setNavigationBarHidden:YES animated:animated];
}

- (void)viewWillDisappear:(BOOL)animated{
    [super viewWillDisappear:animated];
    [self.navigationController setNavigationBarHidden:NO animated:animated];
}


- (void)initView{
  
    UIImageView *headView = [[UIImageView alloc] initWithFrame:CGRectMake(0, 0, SCREEN_W, GETHEIGHT(400))];
    headView.userInteractionEnabled = YES;
    headView.image = [UIImage imageNamed:@"dianpu_bg_defaullt.png"];
    self.tableView.tableHeaderView = headView;
    
    UIImageView *boderImg = [[UIImageView alloc] init];
    boderImg.image = [UIImage imageNamed:@"user_bg_default_nor.png"];
    [headView addSubview:boderImg];
    boderImg.sd_layout.topSpaceToView(headView, GETHEIGHT(104))
    .centerXEqualToView(headView).widthIs(GETWITH(164))
    .heightEqualToWidth();
    if(SCREEN_H == 812){
       headView.height += 89;
        boderImg.sd_layout.topSpaceToView(headView, 142);
    }
    _storeImg = [[UIImageView alloc] init];
    
    [headView addSubview:_storeImg];
    _storeImg.sd_layout.centerYEqualToView(boderImg)
    .centerXEqualToView(headView).widthIs(GETWITH(140))
    .heightEqualToWidth();
    _storeImg.sd_cornerRadius = @(GETWITH(70));
    
    _storeName = [[UILabel alloc] init];
    _storeName.font = font14;
 
   
    _storeName.textAlignment = NSTextAlignmentCenter;
    [headView addSubview:_storeName];
    _storeName.sd_layout.topSpaceToView(_storeImg, GETHEIGHT(20))
    .leftSpaceToView(headView, 0).rightSpaceToView(headView, 0)
    .autoHeightRatio(0);
    
    __weak typeof (self)weakself = self;
    UIButton *merchantBtn = [[UIButton alloc] initWithFrame:CGRectMake(SCREEN_W-GETWITH(110),30, GETWITH(80), GETWITH(80))];
    if(SCREEN_H == 812) merchantBtn.y = 50+46;
    [merchantBtn setImage:[UIImage imageNamed:@"photo_bg8_default"] forState:UIControlStateNormal];
    [merchantBtn handleControlEvent:UIControlEventTouchUpInside withBlock:^(id sender) {
        [weakself.navigationController pushViewController:[MerchantHomeVC new] animated:YES];
    }];
    [headView addSubview:merchantBtn];
    
    
}



- (void)updatePassword{
    ForgotPasswordVC *vc = [[ForgotPasswordVC alloc] init];
    [self.navigationController pushViewController:vc animated:YES];
}

#pragma mark -- UITableViewDelegate

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView{
    return 3;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    if(section==0){
        NSDictionary *dic = [userDefaults objectForKey:@"agentInfo"];
        if([dic[@"extend_fun"] rangeOfString:@"2"].location ==NSNotFound)
            return 2;
        return 3;
    }else if (section == 1){
        NSDictionary *dic = [userDefaults objectForKey:@"agentInfo"];
        if([dic[@"extend_fun"] rangeOfString:@"1"].location ==NSNotFound){
            return 2;
        }
        return 3;
    }
    return 3;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    
    UITableViewCell *cell =[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:@"Cell"];
    UIImageView *imageV = [[UIImageView alloc] initWithFrame:CGRectMake(GETWITH(40), GETHEIGHT(26), GETWITH(40), GETWITH(40))];
    UILabel *titleLab = [[UILabel alloc] initWithFrame:CGRectMake(GETWITH(120), GETHEIGHT(28), 100, GETHEIGHT(36))];
    titleLab.centerY = imageV.centerY;
    titleLab.font = font16;
    titleLab.textColor = [UIColor blackColor];
    
    if(indexPath.section == 0){
        if(indexPath.row == 0){
             titleLab.text = @"商品管理";
            imageV.image = [UIImage imageNamed:@"edit_button5_default_nor.png"];
        }else if (indexPath.row == 1){
            titleLab.text = @"销量统计";
            imageV.image = [UIImage imageNamed:@"user_icon8_default_nor.png"];
        }else if (indexPath.row == 2){
            titleLab.text = @"退款统计";
            imageV.image = [UIImage imageNamed:@"user_icon9_default_nor.png"];
        }
        
    }else if (indexPath.section == 1){
        if(indexPath.row == 0){
            titleLab.text = @"基本信息";
            imageV.image = [UIImage imageNamed:@"user_icon2_default_nor.png"];
        }else if(indexPath.row == 1){
            titleLab.text = @"店铺地图";
            imageV.image = [UIImage imageNamed:@"edit_person_button05_default.png"];
            UILabel *rightLab = [[UILabel alloc] initWithFrame:CGRectMake(SCREEN_W-150,  GETHEIGHT(28), 120, GETHEIGHT(36))];
            rightLab.font = font14;
            rightLab.textColor = UIColorFromRGB(0xEBEBEB);
            rightLab.textAlignment = NSTextAlignmentRight;
            rightLab.text = @"进入地图查看";
            rightLab.centerY = imageV.centerY;
            [cell addSubview:rightLab];
        }else{
            titleLab.text = @"打印设置";
            imageV.image = [UIImage imageNamed:@"user_icon7_default_nor"];

        }
   }else{
        if(indexPath.row == 0){
            titleLab.text = @"通知设置";
            imageV.image = [UIImage imageNamed:@"user_icon4_default_nor.png"];
        }else if (indexPath.row==1){
            titleLab.text = @"修改密码";
            imageV.image = [UIImage imageNamed:@"user_icon1_default_nor.png"];
        }
        else{
            titleLab.text = @"关于我们";
            imageV.image = [UIImage imageNamed:@"user_icon5_default_nor.png"];
        }
    }
    cell.accessoryType = UITableViewCellAccessoryDisclosureIndicator;
    [cell addSubview:titleLab];
    [cell addSubview:imageV];
    return cell;
}



- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
    [tableView deselectRowAtIndexPath:indexPath animated:YES];
    if(indexPath.section == 0){
        if(indexPath.row == 0){
            [self.navigationController pushViewController:[FoodManagementVC new] animated:YES];
        }else if (indexPath.row == 1){
            [self.navigationController pushViewController:[HLBSalesStatisticsVC new] animated:YES];
        }else if (indexPath.row == 2){
            [self.navigationController pushViewController:[HLBReimburseVC new] animated:YES];
        }
    }else if (indexPath.section == 1){
        if(indexPath.row == 0){
            ShopBaseInfoVC *vc = [[ShopBaseInfoVC alloc] init];
            vc.title = @"基本信息管理";
            [self.navigationController pushViewController:vc animated:YES];
        }else if(indexPath.row ==1){
            BaiduMapVC *vc = [[BaiduMapVC alloc] init];
           
            [self.navigationController pushViewController:vc animated:YES];
            
        }else{
            
            PrinterManageVC *vc = [PrinterManageVC new];
            [self.navigationController pushViewController:vc animated:YES];
        }

    }else{
        if(indexPath.row == 0){
          
            NocticeSetViewController *vc = [[NocticeSetViewController alloc] init];
            [self.navigationController pushViewController:vc animated:YES];
//            self.navigationController.tabBarController.selectedIndex = 1;
//            
//            PayOrderViewController *vc = [[PayOrderViewController alloc] initWith:@[] style:1];
//            
//            [self.navigationController.tabBarController.viewControllers[1] pushViewController:vc animated:YES];
        }else if (indexPath.row==1){
            
             [self updatePassword];
        }
        else{
            myWkWebViewController * vc = [[myWkWebViewController alloc] init];
            vc.myurl = [NSString stringWithFormat:@"%@/ShopApi/Html/aboutUs",APP_HOST_ADDRESS];
            vc.toTitle = @"关于我们";
            [self.navigationController pushViewController:vc animated:YES];
        }
    }

}

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
    return GETHEIGHT(90);
}

- (CGFloat)tableView:(UITableView *)tableView heightForFooterInSection:(NSInteger)section{
    return GETHEIGHT(1);
}

- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section{
    return GETHEIGHT(35);
}
- (UIView*)tableView:(UITableView *)tableView viewForFooterInSection:(NSInteger)section{
      UIView *view = [[UIView alloc] initWithFrame:CGRectMake(0, 0, SCREEN_W, GETHEIGHT(1))];
    view.backgroundColor = [UIColor whiteColor];
    return view;
}

- (UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section{
    UIView *view = [[UIView alloc] initWithFrame:CGRectMake(0, 0, SCREEN_W, GETHEIGHT(35))];
    view.backgroundColor = UIColorFromRGB(0xEBEBEB);
    return view;
}

@end
